<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
/**
 * Description of Login
 *
 * @author hasee
 */
class Login extends Controller{
    //put your code here
    public function login(){
        return view("/login/login");
    }
        public function check(Request $request){
        $param=$request->param();
        var_dump($param);
        //$request->except排除某个或某些参数
//        $param=$request->except("code");
        var_dump($param);
        //input获取一个参数
        $users= input("users");
//        echo $users;
        $data['uname']=$param['uname'];
        $data['upwd']=$param['password'];
        var_dump($data);
        //单个条件：Db::name('数据表名')->where('字段1','值1')->select();
        //多个条件：Db::name('数据表名')->where('字段1','值1')->where('字段2','值2')->select();

        if(!captcha_check($param['code'])){
            // 验证失败
            $this->error("验证码错误！", "/index/login/login");
         };
        
        $result=Db::name('users')->where('uname',$data['uname'])
                ->where('upwd', sha1($data['upwd']))
                ->select();
        
        if(!empty($result)){
            $users = $result[0];
            cookie("uname",$users["uname"],time()+60*60*24*7);
            cookie("uid",$users["uid"],time()+60*60*24*7);
            $this->success('恭喜您，登录成功',"index/home/index");
        } else {
            $this->error('用户密码错误',"index/login/login");

        }
        
        return 'login check';
    }
}
